package reflect.access;


import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;

/**
 * @version 1.0
 * @author:罗苏琴
 * @Date 2024/12/2 下午4:54
 */
public class AccessPerSonConstructors {
    public static void main(String[] args) {
        try {
            Class clz = Class.forName("reflect.entity.Person");
            //获取无参构造
            Constructor c1 = clz.getDeclaredConstructor();
            Object obj=new Object();
            obj=c1.newInstance();
            System.out.println(obj);
            //获取无参构造
            Constructor c2 = clz.getDeclaredConstructor(String.class);
            c2.setAccessible(true);
            obj=c2.newInstance("New Person");
            System.out.println(obj);
            //获取无参构造
            Constructor c3 = clz.getDeclaredConstructor(String.class,String.class,String.class);
            c3.setAccessible(true);
            obj=c3.newInstance("New Person","beijing","Hello!");
            System.out.println(obj);
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        } catch (NoSuchMethodException e) {
            throw new RuntimeException(e);
        } catch (InvocationTargetException e) {
            throw new RuntimeException(e);
        } catch (InstantiationException e) {
            throw new RuntimeException(e);
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        }
    }
}